package cn.dglydrpy.study.algorithm.od;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/5 12:23
 * @description
 * 题目描述：给定一个整数，计算该整数有几种连续自然数之和的表达式，并输出每种表达式。
 * 要求自然数个数最少的表达式优先输出，每个表达式中按自然数递增顺序输出，最后输出表达式的个数。
 */
public class Od0004_NumberSplit {

    public static void main(String[] args) {
        
//        int num = 9;

        // 交互式验证
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        while (scanner.hasNextInt()) {
            num = scanner.nextInt();
            System.out.print(myAnswer(num));
            System.out.print(answer1(num));
        } 
        
        
        // 对数器验证
//        boolean succeed = true;
//        for(int num=1;num<=1000;num++){
//            if(!myAnswer(num).equals(answer1(num))){
//                System.out.println("num: " + num);
//                succeed = false;
//                break;
//            }
//        }
//        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
        
    }
    
    private static String myAnswer(int num){
        
        StringBuilder ans = new StringBuilder();
        int count = 0;
        ArrayList<Integer> temp = null;
        
        if(num == 1){
            count = 1;
            ans.append(num + "=" + num + "\n");
            ans.append("Result:"+ count +"\n");
            return ans.toString();
        }
        
        for(int i=1;i<num;i++){
            int avg = num/i;
            int sum  = 0;
            
            int first = 0;

            int m = 0;
            while(m < i){
                int j = i % 2 == 1 ? avg - i/2 : avg - i/2 + 1; // 奇数或偶数
                if(j<=0) break;
                sum = sum + j + m;
                m++;

                first = j;
            }
            
            if(sum == num){
                count ++;
                ans.append(num).append("=");
                for(int k = first;k<first + i;k++){
                    if(k == first){
                        ans.append(k);
                    }else{
                        ans.append("+").append(k);
                    }
                }
                ans.append("\n");
            }
        }
        ans.append("Result:"+ count +"\n");
        
        return ans.toString();
    };


    public static String answer1(int num) {
        StringBuilder ans = new StringBuilder();

        // 计算表达式的个数
        int count = 1;
        ans.append(num + "=" + num + "\n");
        
        for (int i = num / 2; i > 0; i--) {
            int sum = 0;
            int j = i;
            while (sum < num && j <= num) {
                sum += j;
                j++;
            }
            if (sum == num) {
                ans.append(num + "=");
                for (int k = i; k < j; k++) {
                    ans.append(k);
                    if (k < j - 1) {
                        ans.append("+");
                    } else {
                        ans.append("\n");
                    }
                }
                count++;
            }
        }
        ans.append("Result:" + count + "\n");
        return ans.toString();
    }
}
